package com.ngmaster.socket.handler;



import com.alibaba.fastjson.JSON;
import com.ngmaster.socket.MessageHandler;
import com.ngmaster.socket.callback.CallbackGainDeviceId;
import com.ngmaster.socket.message.GainDeviceIdMessage;
import com.ngmaster.socket.util.HttpRequestUtil;
import lombok.extern.slf4j.Slf4j;
import okhttp3.RequestBody;
import org.springframework.stereotype.Component;

/**
 * 心跳协议处理，设备终端发上来的心跳数据，存储到db中，以供后台查询使用
 * */
@Slf4j
@Component
public class GainDeviceIdHandler extends MessageHandler<GainDeviceIdMessage> {

    @Override
    public boolean handleMessage(GainDeviceIdMessage protocolMessage) {
        protocolMessage.setByteArray(null);
        protocolMessage.setImeiBytes(null);
        String msgJson = JSON.toJSONString(protocolMessage);

        RequestBody requestBody = RequestBody.create(this.getJsonMetaType(), msgJson);
        HttpRequestUtil.asyncPost(this.getApiUrl(), new CallbackGainDeviceId(), requestBody);
        return true;
    }

    @Override
    public String getApiUrl() {
        return super.getApiUrl() + "gainDeviceId";
    }
}
